package com.tianji.learning.listener;

import com.tianji.common.constants.MqConstants;
import com.tianji.common.domain.dto.LikedTimesDTO;
import com.tianji.common.domain.dto.UsedTimesDTO;
import com.tianji.learning.domain.po.GatheredNote;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.domain.po.TjNote;
import com.tianji.learning.service.IInteractionReplyService;
import com.tianji.learning.service.ITjNoteService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class GatheredNoteCountListener {
    @Autowired
    private ITjNoteService noteService;

    @RabbitListener(bindings = @QueueBinding(
            value  = @Queue(name = MqConstants.Queue.UESD_TIMES_QUEUE),
            exchange = @Exchange(name=MqConstants.Exchange.GATHERED_NOTE_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.USED_TIMES_KEY_TEMPLATE
    ))
    public void onGatherNote(UsedTimesDTO dto){
        TjNote tjNote = new TjNote();
        tjNote.setId(dto.getNoteId());
        tjNote.setUsedTimes(dto.getUsedTimes()<=0?0:dto.getUsedTimes());
        tjNote.setUpdateTime(LocalDateTime.now());
        noteService.updateById(tjNote);
    }
//    public void onGatherNote(List<UsedTimesDTO> dtoList){
//        List<TjNote> gatheredNotesList = new ArrayList<>();
//        for (UsedTimesDTO dto : dtoList) {
//            TjNote tjNote = new TjNote();
//            tjNote.setId(dto.getNoteId().intValue());
//            tjNote.setUsedTimes(dto.getUsedTimes()<=0?0:dto.getUsedTimes());
//            gatheredNotesList.add(tjNote);
//        }
//        noteService.updateBatchById(gatheredNotesList);
//
//    }
}
